﻿using DevExpress.XtraBars;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class TopGecmisi : XPopupForm
    {
        int RollId = 0;
        List<int> RollIds = new List<int>();
        public TopGecmisi()
        {
            InitializeComponent();
        }

        public TopGecmisi(int rollId)
            : this()        
        {
            RollId = rollId;
        }

        private void TopGecmisi_Load(object sender, EventArgs e)
        {
            btnOnceki.Enabled = false;
            

            tv.NodeMouseClick += tv_NodeMouseClick;
            tv.MouseMove += tv_MouseMove;

            beiTopId.EditValueChanged += beiTopId_EditValueChanged;
            if(RollId>0)
                beiTopId.EditValue = RollId;
        }

        void beiTopId_EditValueChanged(object sender, EventArgs e)
        {
            RollIds.Clear();
            tv.Nodes.Clear();
            RollId = beiTopId.EditValue.ToInt();
            RollIds.Add(RollId);

            DataSet ds = ExecuteDataSet("GetRollInfos", RollId);
            if (ds.Tables[0].Rows.Count > 0)
                GetTreeNode(ds);
        }

        void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag.ToStr() == "1")
            {
                RollId = e.Node.Name.ToInt();
                RollIds.Add(RollId);
                tv.Nodes.Clear();
                DataSet ds = ExecuteDataSet("GetRollInfos", RollId);
                GetTreeNode(ds);

                bool visible = RollIds.Count > 1;
                btnOnceki.Enabled = visible;
            } 
        }

        void tv_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePos = tv.PointToClient(Cursor.Position);
            TreeNode nodeOver = tv.GetNodeAt(mousePos);

            if (nodeOver == null)
            {
                this.Cursor = Cursors.Default;
            }
            else
            {
                if (nodeOver.Tag.ToStr() == "1")                
                    this.Cursor = Cursors.Hand;                 
                else
                    this.Cursor = Cursors.Default;
            }
        }

        private void GetTreeNode(DataSet ds)
        {
            //lblHeader.Caption = "Top No. " + RollId.ToString();
            DataRow drPhoto = ds.Tables[0].Rows[0];
            if (drPhoto["Photo"] != DBNull.Value)
            {
                peScan.Visible = true;
                MemoryStream ms = new MemoryStream((byte[])drPhoto["Photo"]);
                Image pimg = Image.FromStream(ms);
                peScan.Image = pimg;
                //img.Size = pimg.Size;
                //peScan.Properties.SizeMode = PictureSizeMode.Stretch;
            }

            TreeNode rootNode = new TreeNode(RollId.ToString());
            foreach (DataTable dt in ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    string NodeName = dt.Rows[0]["NodeName"].ToString();
                    TreeNode childNode = new TreeNode(NodeName);
                    childNode.ForeColor = Color.Crimson;
                    childNode.NodeFont = new Font("Tahoma", 8, FontStyle.Bold);
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dt.Rows.Count > 1 && dt.Columns.Count > 2)
                        {
                            string nodeRowCaption = dr[1].ToString();
                            TreeNode nodeRow = new TreeNode(nodeRowCaption);
                            foreach (DataColumn dc in dt.Columns)
                            {
                                if (dc.ColumnName != "NodeName" && dc.ColumnName != "Photo" && dr[dc].ToStr() != "" && dc.Ordinal > 1)
                                {
                                    string node = dc.ColumnName + " : " + dr[dc].ToString();
                                    TreeNode nodeExp = new TreeNode(node, 1, 0);
                                    nodeExp.Name = dr[dc].ToString();
                                    if (dc.ColumnName.Contains("Top"))
                                    {
                                        nodeExp.ForeColor = Color.RoyalBlue;
                                        nodeExp.NodeFont = new Font("Tahoma", 8, FontStyle.Underline);

                                    }
                                    nodeRow.Nodes.Add(nodeExp);
                                }
                            }
                            nodeRow.Expand();
                            childNode.Nodes.Add(nodeRow);
                        }
                        else
                        {
                            foreach (DataColumn dc in dt.Columns)
                            {
                                if (dc.ColumnName != "NodeName" && dc.ColumnName != "Photo" && dr[dc].ToStr() != "")
                                {
                                    string node = dc.ColumnName + " : " + dr[dc].ToString();
                                    TreeNode nodeExp = new TreeNode(node, 1, 0);
                                    nodeExp.Name = dr[dc].ToString();
                                    if (dc.ColumnName.Contains("Top"))
                                    {
                                        //KG
                                        nodeExp.ForeColor = Color.RoyalBlue;
                                        nodeExp.NodeFont = new Font("Tahoma", 8, FontStyle.Underline);
                                        nodeExp.Tag = "1";
                                    }
                                    //nodeExp.Expand();
                                    childNode.Nodes.Add(nodeExp);
                                }
                            }
                        }
                    }
                    childNode.Expand();
                    rootNode.Nodes.Add(childNode);
                }
            }
            rootNode.Expand();
            tv.Nodes.AddRange(new TreeNode[] { rootNode });            
        }

      

        private void btnOnceki_ItemClick(object sender, ItemClickEventArgs e)
        {
            RollIds.RemoveAt(RollIds.Count - 1);
            RollId = RollIds[RollIds.Count - 1];

            tv.Nodes.Clear();
            DataSet ds = ExecuteDataSet("GetRollInfos", RollId);
            GetTreeNode(ds);

            bool visible = RollIds.Count > 1;
            btnOnceki.Enabled = visible;
        }
    }
}